package org.osami.gateway.watteco.internal.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;

import org.osami.gateway.watteco.interfaces.ISwitch;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 09/12/10
 * Time: 18:08
 * To change this template use File | Settings | File Templates.
 */
public class WattecoFrame extends JFrame {

    private JLabel status;
    private JLabel title;
    private ImageIcon blueIcon;
    private ImageIcon greenIcon;
    private ImageIcon redIcon;

    public WattecoFrame(final ISwitch model) throws HeadlessException {
        super();

        JPanel container = new JPanel();
        add(container);

        // Title
        title = new JLabel();
        container.add(title);

        // Panel State
        JPanel statusPanel = new JPanel();
        container.add(statusPanel);

        URL blue = getClass().getResource("blue.gif");
        blueIcon = new ImageIcon(blue);
        URL green = getClass().getResource("green.gif");
        greenIcon = new ImageIcon(green);
        URL red = getClass().getResource("red.gif");
        redIcon = new ImageIcon(red);

        status = new JLabel();
        status.setIcon(blueIcon);
        statusPanel.add(status);

        JButton button = new JButton();
        container.add(button);
        button.setText("Change state");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    model.toggleSwitch();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void switchOn() {
        status.setIcon(greenIcon);
    }

    public void switchOff() {
        status.setIcon(redIcon);
    }

    public void setSerial(String serial) {
        title.setText("Watteco[" + serial + "] ");
    }
}
